﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Presentation.Module.Setup.Interfaces.ViewModels;
using Presentation.Module.Setup.Interfaces.Services;
using Microsoft.Practices.Prism.Regions;
using Presentation.Module.Setup.Models;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Input;
using System.ComponentModel;

namespace Presentation.Setup.Views
{
    /// <summary>
    /// View model for the and IAddSkillView
    /// </summary>
    public class AddSkillViewModel : IAddSkillViewModel, INotifyPropertyChanged
    {
        #region Private Fields
        private Skill _CurrentSkill; 
        #endregion

        public AddSkillViewModel(ISkillsService skillService, IRegionManager regionManager)
        {
            this.SkillService = skillService;
            this.RegionManager = regionManager;

            this.Commit = new DelegateCommand(OnCommit);
            this.Cancel = new DelegateCommand(OnCancel);
        }

        #region Public Properties
        public Skill CurrentSkill
        {
            get
            {
                return _CurrentSkill;
            }
            set
            {
                if (value != null)
                {
                    _CurrentSkill = value;
                    RaiseOnPropertyChanged("CurrentSkill");
                }
            }
        }
        public Module.Setup.Interfaces.Views.IAddSkillView View { get; set; }
        #endregion

        #region Private Properties
        private ISkillsService SkillService { get; set; }
        private IRegionManager RegionManager { get; set; }        
        #endregion

        #region Commands and Command Implementations
        public ICommand Commit { get; set; }
        public ICommand Cancel { get; set; }

        private void OnCommit()
        {
            this.SkillService.AddSkill(this.CurrentSkill);
            View.Exit();
        }

        private void OnCancel()
        {
            View.Exit();
        }

        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}
